﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using 纸片人.win2dEffectControl;
using 纸片人.win2dEffectControl.DataClass;

// The Templated Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234235

namespace 纸片人.CalculateControls
{
    public  class RectSlider : RangeControl
    {



        #region Rect     
        /// <summary>
        /// 获取或设置Rect的值
        /// </summary>  
        public Rect Rect
        {
            get { return (Rect)GetValue(RectProperty); }
            set { SetValue(RectProperty, value); }
        }

        /// <summary>
        /// 标识 Rect 依赖属性。
        /// </summary>
        public static readonly DependencyProperty RectProperty =
            DependencyProperty.Register("Rect", typeof(Rect), typeof(RectSlider), new PropertyMetadata(new Rect(), OnRectChanged));

        private static void OnRectChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            RectSlider target = obj as RectSlider;
            Rect oldValue = (Rect)args.OldValue;
            Rect newValue = (Rect)args.NewValue;
            if (oldValue != newValue)
                target.OnRectChanged(oldValue, newValue);
        }

        protected virtual void OnRectChanged(Rect oldValue, Rect newValue)
        {
        }
        #endregion




        protected TextBoxSlider oneSlider;
        protected TextBoxSlider twoSlider;
        protected TextBoxSlider threeSlider;
        protected TextBoxSlider fourSlider;
        public RectSlider()
        {
            this.DefaultStyleKey = typeof(RectSlider);
        }

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            oneSlider = GetTemplateChildFormName<TextBoxSlider>(nameof(oneSlider));
            twoSlider = GetTemplateChildFormName<TextBoxSlider>(nameof(twoSlider));
            threeSlider = GetTemplateChildFormName<TextBoxSlider>(nameof(threeSlider));
            fourSlider = GetTemplateChildFormName<TextBoxSlider>(nameof(fourSlider));
        }

        protected override void OnexporterDrag(UIElement sender, DragStartingEventArgs args)
        {
            OnDragExportInvoke();
            OutputData od = new OutputData();
            od.Type = OutputType.Vector4;
            od.Exporter = this;
            od.ExportConnectElement = (ExporterDragControl)sender;
            od.ExporterInfo = nameof(Rect);
            EffectOutputControl.outputData = od;
        }

        protected override void OnProviderEnter(object sednder, DragEventArgs e)
        {
            if (EffectOutputControl.outputData != null && EffectOutputControl.outputData.Type == OutputType.Vector4)
            {
                e.AcceptedOperation = DataPackageOperation.Copy;
                e.DragUIOverride.Caption = "拖进来了";
            }
        }

        protected override void OnProviderDrop(object sender, DragEventArgs e)
        {
            var outputData = EffectOutputControl.outputData;
            //判断是否形成了闭环了
            if (IsChartLoop(outputData.Exporter))
                return;
            if (outputData != null && outputData.Type == OutputType.Vector4)
            {
                var exporter = EffectOutputControl.outputData.Exporter;
                if (outputData.ExporterInfo is string str)
                {
                    exproterValueName = str;
                }
                else
                {
                    return;
                }
                (providerDrop as ProviderDropControl)?.SetConnectState();
                CreateConnect(ref oneConnectElement, providerDrop);
            }
        }

        protected override void Timer_Tick(object sender, object e)
        {
            if (oneConnectElement != null)
            {

                var exporter = oneConnectElement.Exporter;

                try
                {
                    var value = GetProvideValue(exporter, exproterValueName);
                    if (value is Rect v2)
                    {
                        if (v2 != Rect)
                        {
                            Rect = v2;
                        }
                        if (oneSlider != null && oneSlider.Value != Rect.X)
                        {
                            oneSlider.Value = Rect.X;
                        }
                        if (twoSlider != null && twoSlider.Value != Rect.Y)
                        {
                            twoSlider.Value = Rect.Y;
                        }
                        if (threeSlider != null && threeSlider.Value != Rect.Width)
                        {
                            threeSlider.Value = Rect.Y;
                        }
                        if (fourSlider != null && fourSlider.Value != Rect.Height)
                        {
                            fourSlider.Value = Rect.Y;
                        }
                    }
                    else if (value is Vector4 v4)
                    {
                        if (v4.X != Rect.X|| v4.Y!= Rect.Y|| v4.Z!= Rect.Width||v4.W!=Rect.Height)
                        {
                            Rect =new Rect(v4.X, v4.Y, v4.Z, v4.W);
                        }
                        if (oneSlider != null && oneSlider.Value != Rect.X)
                        {
                            oneSlider.Value = Rect.X;
                        }
                        if (twoSlider != null && twoSlider.Value != Rect.Y)
                        {
                            twoSlider.Value = Rect.Y;
                        }
                        if (threeSlider != null && threeSlider.Value != Rect.Width)
                        {
                            threeSlider.Value = Rect.Y;
                        }
                        if (fourSlider != null && fourSlider.Value != Rect.Height)
                        {
                            fourSlider.Value = Rect.Y;
                        }
                    }
                }
                catch
                {
                    exporter.Disconnect(oneConnectElement);
                    exporter = null;
                }

            }
            else
            {

                if (oneSlider.Value != Rect.X || fourSlider.Value != Rect.Y || threeSlider.Value != Rect.Width || fourSlider.Value != Rect.Height)
                {
                    Rect = new Rect((float)oneSlider.Value, (float)twoSlider.Value, (float)threeSlider.Value, (float)fourSlider.Value);
                }
            }
        }
    }
}
